package com.penjin.android.activity.regist;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.jungly.gridpasswordview.GridPasswordView;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.penjin.android.R;
import com.penjin.android.constants.HttpConstants;
import com.penjin.android.http.HttpService;
import com.penjin.android.message.view.EaseSwitchButton;
import com.penjin.android.utils.HttpUtil;
import com.penjin.android.utils.ToastHelper;
import com.penjin.android.view.CustomProgressDialog;
import com.penjin.android.view.TitleBarView;
import com.penjin.android.widget.GetCodeView;

import org.apache.http.Header;

/**
 * Created by Administrator on 2016/4/18.
 */
public class FindPasswordActivity extends FragmentActivity implements View.OnClickListener {

    TitleBarView titleBarView;
    CustomProgressDialog progressDialog;
    EditText phone;
    GetCodeView getCode;
    GridPasswordView pswView;
    HttpService httpService;
    TextHttpResponseHandler handler;
    TextHttpResponseHandler getCodeHandler;
    View next;

    EditText newPassword;
    EditText confirmPassword;
    EaseSwitchButton switch_btn;
    boolean showPassword = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_findpwd);
        initView();
        initHttp();
    }

    private void initView() {
        titleBarView = (TitleBarView) findViewById(R.id.titleBarView);
        titleBarView.setTitleBarListener(new TitleBarView.TitleBarListener() {
            @Override
            public void left(View view) {
                finish();
            }

            @Override
            public void center(View view) {

            }

            @Override
            public void right(View view) {

            }
        });
        progressDialog = CustomProgressDialog.createDialog(this);
        progressDialog.setMessage("请稍后...");
        getCode = (GetCodeView) findViewById(R.id.getCode);
        getCode.setOnClickListener(this);
        pswView = (GridPasswordView) findViewById(R.id.pswView);
        pswView.setPasswordVisibility(true);
        phone = (EditText) findViewById(R.id.phone);
        next = findViewById(R.id.next);
        next.setOnClickListener(this);
        switch_btn = (EaseSwitchButton) findViewById(R.id.switch_btn);
        switch_btn.setOnClickListener(this);
        newPassword = (EditText) findViewById(R.id.newPassword);
        confirmPassword = (EditText) findViewById(R.id.confirmPassword);
    }

    private void initHttp() {
        httpService = HttpService.getInstance(this.getApplicationContext());
        handler = new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                progressDialog.dismiss();
                if (s != null) {
                    System.out.println(s);
                }
                ToastHelper.showServerError(getApplicationContext());
            }

            @Override
            public void onSuccess(int i, Header[] headers, String s) {
                progressDialog.dismiss();
                ToastHelper.showMessage(s, getApplicationContext());
                finish();
            }
        };
        getCodeHandler = new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                progressDialog.dismiss();
                ToastHelper.showServerError(getApplicationContext());
            }

            @Override
            public void onSuccess(int i, Header[] headers, String s) {
                progressDialog.dismiss();
                ToastHelper.showMessage(s, getApplicationContext());
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.getCode:
                if (HttpUtil.isNetworkAvailable(this)) {
                    String str = phone.getEditableText().toString();
                    if (str == null || str.equals("")) {
                        ToastHelper.showMessage("手机号码不能为空", getApplicationContext());
                        return;
                    } else {
                        if (getCode.isRunning) {
                            ToastHelper.showMessage("验证码已发送，请稍后获取", getApplicationContext());
                        } else {
                            getCode.start();
                            getCode();
                        }
                    }
                } else {
                    ToastHelper.showNoNetwork(getApplicationContext());
                }
                break;
            case R.id.next:
                if (HttpUtil.isNetworkAvailable(this)) {
                    String code = pswView.getPassWord();
                    if (code == null || code.equals("") || code.length() != 4) {
                        ToastHelper.showMessage("请输入4位验证码", getApplicationContext());
                        return;
                    }
                    String pwd = newPassword.getEditableText().toString();
                    String confirmPwd = confirmPassword.getEditableText().toString();
                    int result = checkPassword(pwd, confirmPwd);
                    if (result != 0) {
                        return;
                    } else {
                        //currentUser.setPassword(confirmPassword.getEditableText().toString());
                        progressDialog.show();
                        launchRequest();
                    }
                } else {
                    ToastHelper.showNoNetwork(this);
                }
                break;
            case R.id.switch_btn:
                showPassword(this.showPassword);
                if (this.showPassword == true) {
                    this.switch_btn.openSwitch();
                    this.showPassword = false;
                } else {
                    this.switch_btn.closeSwitch();
                    this.showPassword = true;
                }
                break;
        }
    }

    private int checkPassword(String pwd, String confirmPwd) {
        if (pwd == null || pwd.equals("")) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return 1;
        }
        if (pwd.length() < 6) {
            Toast.makeText(this, "密码长度至少为6位", Toast.LENGTH_SHORT).show();
            return 2;
        }

        if (!pwd.equals(confirmPwd)) {
            Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            return 3;
        }
        return 0;
    }

    private void showPassword(boolean show) {
        if (show) {
            this.newPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            this.confirmPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            this.newPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            this.confirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }

    }

    /**
     * 发起获取验证码请求
     */
    private void getCode() {
        progressDialog.show();
        RequestParams requestParams = new RequestParams();
        requestParams.put("PhoneNumber", phone.getEditableText().toString());
        try {
            httpService.postRequestWithHandle(this, HttpConstants.HOST + HttpConstants.AppSendCode, requestParams, this.getCodeHandler);
        } catch (Exception e) {
            progressDialog.dismiss();
            e.printStackTrace();
            Toast.makeText(this, "网络连接异常!,请重试", Toast.LENGTH_SHORT).show();
        }
    }

    private void launchRequest() {
        RequestParams requestParams = new RequestParams();
        requestParams.put("userName", phone.getEditableText().toString());
        requestParams.put("code", pswView.getPassWord());
        requestParams.put("password", confirmPassword.getEditableText().toString());
        progressDialog.show();
        try {
            httpService.postRequest(getApplicationContext(), HttpConstants.HOST + HttpConstants.AppLookPassword, requestParams, handler);
        } catch (Exception e) {
            e.printStackTrace();
            progressDialog.dismiss();
        }
    }
}
